﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.UI;

namespace blackhouse.Web.UI
{
    public class MultiPageCounter : IPageCounter
    {

        #region Fields

        private List<PageCounterControl> controls = new List<PageCounterControl>();
        private int currentPage = 1;
        private string formatLastPage_priv = "{0}";
        private string formatNextPage_priv = "{0}";
        private string formatPage_priv = "{0}";
        private string formatSelectedPage_priv = "{0}";

        private int itemsOnPage = 100;
        private int totalItems = 0;

        #endregion

        #region Properties

        /// <summary>
        /// Format HTML standardowej, ostatniej strony, np.: <![CDATA[ <a href="Default.aspx?page={0}">{0}</a>]]>. {0} jest to numer aktualnie budowanej strony
        /// </summary>
        public string FormatLastPage
        {
            get { return this.formatLastPage_priv; }
            set
            {
                this.formatLastPage_priv = value;
                this.updateCurrentControls();
            }
        }

        /// <summary>
        /// Format HTML standardowej, następnej strony, np.: <![CDATA[ <a href="Default.aspx?page={0}">{0}</a>]]>. {0} jest to numer aktualnie budowanej strony
        /// </summary>
        public string FormatNextPage
        {
            get { return this.formatNextPage_priv; }
            set
            {
                this.formatNextPage_priv = value;
                this.updateCurrentControls();
            }
        }

        /// <summary>
        /// Format HTML standardowej strony, np.: <![CDATA[ <a href="Default.aspx?page={0}">{0}</a>]]>. {0} jest to numer aktualnie budowanej strony
        /// </summary>
        public string FormatPage
        {
            get { return this.formatPage_priv; }
            set
            {
                this.formatPage_priv = value;
                this.updateCurrentControls();
            }
        }

        /// <summary>
        /// Format HTML standardowej, wybranej strony, np.: <![CDATA[ <a href="Default.aspx?page={0}">{0}</a>]]>. {0} jest to numer aktualnie budowanej strony
        /// </summary>
        public string FormatSelectedPage
        {
            get { return this.formatSelectedPage_priv; }
            set
            {
                this.formatSelectedPage_priv = value;
                this.updateCurrentControls();
            }
        }


        #endregion

        #region Methods

        public void AddControl(PageCounterControl control)
        {
            this.controls.Add(control);
            this.updateControl(control);
        }

        public void Load()
        {
            foreach (PageCounterControl control in this.controls)
                control.Controller.Load();
        }

        private void updateCurrentControls()
        {
            foreach (PageCounterControl control in this.controls)
            {
                this.updateControl(control);
            }
        }

        private void updateControl(PageCounterControl control)
        {
            control.CurrentPage = this.GetCurrentPage();
            control.ItemsOnPage = this.GetItemsOnPage();
            control.SetTotalItems(this.totalItems);
            control.FormatLastPage = this.FormatLastPage;
            control.FormatNextPage = this.FormatNextPage;
            control.FormatPage = this.FormatPage;
            control.FormatSelectedPage = this.FormatSelectedPage;
        }

        #endregion

        #region IPageCounter Members

        public int GetCurrentPage()
        {
            return this.currentPage;
        }

        public int GetItemsOnPage()
        {
            return this.itemsOnPage;
        }

        public void SetTotalItems(int itemsCount)
        {
            this.totalItems = itemsCount;
            this.updateCurrentControls();
        }

        #endregion

    }
}